﻿package bakudo.engines.mapEngine.view.scene {
	import bakudo.engines.mapEngine.model.actors.ModelActor;
	import bakudo.engines.mapEngine.model.WorldModel;
	import bakudo.engines.mapEngine.view.scene.actors.BackgroundTileActor;
	import bakudo.engines.mapEngine.view.scene.actors.SceneActor;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.LineScaleMode;
	import flash.display.Scene;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;	
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;

	/**
	 * @author Micha� Rabi�ski
	 */
	public class Scene2D extends MovieClip {

		/** Layer z tlem czyli plytkami podlogi */
		public var floorLayer : Bitmap;
		
		/** Layer z obiektami z ktorymi mozna dokonac kolizji */
		public var obstaclesLayer : Bitmap;
		
		/** Layer z postaciami */
		public var charactersLayer : Bitmap;
		
		
		private var _worldModel : WorldModel;	
		
		private var testLayer : Bitmap;
		
		private var firstStart : Boolean;
		
		private var camPointer : Shape;
		
		
		
		public function Scene2D() {
			addEventListener(Event.ADDED_TO_STAGE, onATS, false, 0, true);
		}
		
		public function set worldModel(_worldModel : WorldModel):void {
			this._worldModel = _worldModel;
		}

		
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PROTECTED METHODS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function setup():void {
			var m : WorldModel = _worldModel;
			
			floorLayer = new Bitmap(new BitmapData(_worldModel.viewWidth, _worldModel.viewHeight,true,0x00000000));
			floorLayer.x = 0;
			floorLayer.y = 0;
			addChild(floorLayer);
			
			charactersLayer = new Bitmap(new BitmapData(_worldModel.viewWidth, _worldModel.viewHeight,true,0x00000000));
			charactersLayer.x = 0;
			charactersLayer.y = 0;
			addChild(charactersLayer);
		
			var border : Shape = new Shape;
			var g : Graphics = border.graphics;
			g.lineStyle(1, 0xFF0000, 1);
			g.drawRect(0, 0, _worldModel.viewWidth, _worldModel.viewHeight);
			addChild(border);
			
			var camPointer2 = new Shape;
			g = camPointer2.graphics;
			g.beginFill( 0x0000FF,1);
			g.drawCircle(0, 0, 5);
			addChild(camPointer2);		
			camPointer2.x = _worldModel.viewWidth * .5;
			camPointer2.y = _worldModel.viewHeight * .5;
			
			// test layer
			var bdata : BitmapData = new BitmapData(m.mapWidth < 2000 ? m.mapWidth : 2000, m.mapHeight < 2000 ? m.mapHeight : 2000, true, 0x00000000);
			testLayer = new Bitmap(bdata);
			addChild(testLayer);
			testLayer.x = 600;
			testLayer.y = 0;
			camPointer = new Shape;
			var g : Graphics = camPointer.graphics;
			g.beginFill( 0x0000FF,1);
			g.drawCircle(0, 0, 5);
			addChild(camPointer);		
		
		}
		
		public function updateMap(tlActor : ModelActor = null,actors:Array/*of ModelActor*/ = null):void {
			var bwidth : Number = 1000;
			var bheight : Number = 1000;
			var	bdata : BitmapData = testLayer.bitmapData;
			var actor : ModelActor;
			var rowCount : uint = 0;
			var columnCount : uint = 0;
			var bd : BitmapData;
			var pos : Point = new Point;
			var i : uint = 0;
			var n : uint = _worldModel.bgColumnsCount * _worldModel.bgRowsCount;
			camPointer.x = testLayer.x + _worldModel.cameraPosition.x;
			camPointer.y = testLayer.y + _worldModel.cameraPosition.y;
				while (i < n) {
					actor = _worldModel.mapGrid[rowCount][columnCount];
					if(actor){
						bd = _worldModel.getViewByClassName(actor.viewClassName);
						bdata.copyPixels(bd, new Rectangle(0, 0, bd.width-1, bd.height-1), pos);
					}
					
					
					/*
					for (var j in actors) {
						if ((actors[j] as ModelActor) == actor) {
							bd = new BitmapData(_worldModel.gridSize, _worldModel.gridSize,false,0xCCFFFFFF);
							}
					}*/
					if (columnCount < _worldModel.bgColumnsCount-1) {
						pos.x += _worldModel.gridSize;
						columnCount++;
					}else {
						if (rowCount < _worldModel.bgRowsCount- 1) {
							pos.x = 0;
							columnCount = 0;
							pos.y += _worldModel.gridSize;
							rowCount ++;
						}
					}
					i++;
				}
		}
		
		
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PUBLIC METHODS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PRIVATE LISTENERS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onATS(event : Event) : void {
			removeEventListener(Event.ADDED_TO_STAGE, onATS);
			
		}
	}
}
